import java.io.Serializable;

import java.io.*;

/**
 * A single PhoneNum represents a 10 digit phone number as a String
 * @author Joshua Zeiser
 *
 */
public class PhoneNum implements Comparable<PhoneNum>, Serializable{

	private String phoneNumber;

	/**
	 * Constructor, takes String for phoneNumber
	 * @param phoneNumber
	 */
	public PhoneNum(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	/**
	 * Empty constructor, sets phoneNumber to empty string
	 */
	public PhoneNum() {
		this.phoneNumber = "";
	}
	
	/**
	 * @return String the phoneNumber
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * @param phoneNumber the phoneNumber to set
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	/**
	 * Overrides default toString method
	 * Returns String representation of a phoneNumber, formatted with dashes
	 * @return the phoneNumber as String
	 */
	@Override
	public String toString()
	{
		String formattedPhoneNum = new String(this.phoneNumber);
		if(this.phoneNumber.length() == 11) {
			return formattedPhoneNum = this.phoneNumber.substring(0, 1) + "-" + this.phoneNumber.substring(1, 4) + "-" + this.phoneNumber.substring(4, 7) + "-" + this.phoneNumber.substring(7);
		}
		else if(this.phoneNumber.length() == 10) {
			return formattedPhoneNum = this.phoneNumber.substring(0, 3) + "-" + this.phoneNumber.substring(3, 6) + "-" + this.phoneNumber.substring(6);
		}
		else if(this.phoneNumber.length() == 7) {
			return formattedPhoneNum = this.phoneNumber.substring(0, 3) + "-" + this.phoneNumber.substring(3);
		}
		// worst case it doesn't fit formatting rules and just returns the number
		else {
			return formattedPhoneNum;
		}
	}

	/**
	 * Overrides compareTo method to allow comparison of two PhoneNum objects
	 * ***Not used for task 3 but implemented for early testing***
	 * @param p
	 * @return int
	 */
	@Override public int compareTo(PhoneNum p) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;
		
		if(this.equals(p)) return EQUAL;
		
		int comparison = this.phoneNumber.compareTo(p.getPhoneNumber());
		if(comparison != 0) return comparison;
		
		return EQUAL;
	}
	
	/**
	 * Overrides Object equals method, allows equality comparison of PhoneNum objects
	 * ***Not used for task 3 but implemented for early testing***
	 * @param p
	 * @return boolean
	 */
	@Override public boolean equals(Object p) {
		if(this == p) return true;
		if(!(p instanceof EMail)) return false;
		
		PhoneNum p2 = (PhoneNum)p;
		
		return (this.phoneNumber.equals(p2.getPhoneNumber()));
	}
	
}
